//
//   ______    ______    ______
//  /\  __ \  /\  ___\  /\  ___\
//  \ \  __<  \ \  __\_ \ \  __\_
//   \ \_____\ \ \_____\ \ \_____\
//    \/_____/  \/_____/  \/_____/
//
//  Powered by BeeFramework
//
//
//  OrderM.m
//  Pizza
//
//  Created by sendInfo on 2015-04-07.
//  Copyright (c) 2015年 sendInfo. All rights reserved.
//

#import "OrderM.h"

#pragma mark -

@interface OrderM(){

    int _page;
}

@end

@implementation OrderM

- (void)load
{
    self.ordersArray = [[NSMutableArray alloc] initWithCapacity:0];
    //[self loadCache];
}

- (void)unload
{
    [self saveCache];
}

- (NSString *)cacheKey
{
    // You should set an identified key if this model is shared.
    return NSStringFromClass([self class]);
}

- (void)loadCache
{
    NSData * data = bee.system.fileCache[self.cacheKey];
    if ( data )
    {
        [self.ordersArray removeAllObjects];
        [self.ordersArray addObjectsFromArray:[NSObject objectsFromAny:data]];
    }
}

- (void)saveCache
{
    bee.system.fileCache[self.cacheKey] = [self.ordersArray objectToData];
}

- (void)clearCache
{
    bee.system.fileCache[self.cacheKey] = nil;
    
    [self.ordersArray removeAllObjects];
    
    self.loaded = NO;
}

#pragma mark -

- (void)firstPage
{
    
    _page = 1;
    [self gotoPage:1];
    
}
- (void)nextPage
{
    
    [self gotoPage:self.ordersArray.count/10+1];
    _page = self.ordersArray.count/10+1;
    
}
- (void)gotoPage:(NSUInteger)page
{
    self.CANCEL_MSG(HttpAPI.current_order);
    self.CANCEL_MSG(HttpAPI.history_order);

    if ([self.orderType isEqualToString:@"history"]) {
        
        NSDictionary *dic = @{@"token":[SendIFAPPDefault shareAppDefault].userToken,@"pageSize":@"10",@"pageNumber":[NSString stringWithFormat:@"%d",_page]};
        self.MSG(HttpAPI.history_order).INPUT(@"body",dic);
        
    }else{
    
        NSDictionary *dic = @{@"token":[SendIFAPPDefault shareAppDefault].userToken,@"pageSize":@"10",@"pageNumber":[NSString stringWithFormat:@"%d",_page],@"stauts":@"1"};
        self.MSG(HttpAPI.current_order).INPUT(@"body",dic);
    }

}

ON_MESSAGE3(HttpAPI, current_order, msg)
{
    
    if (msg.sending) {
        
        
        [self sendUISignal:self.RELOADING];
        
    }else {
        
        
        if (msg.succeed) {
            
            Status *sta = msg.GET_OUTPUT(@"status");
            if ([sta.code intValue] != 10000) {
                
                [self presentFailureTips:sta.message];
                
            }
            
            if (_page == 1) {
                
                [self.ordersArray removeAllObjects];
                self.ordersArray = msg.GET_OUTPUT(@"orders");
                
            }else{
                
                [self.ordersArray addObjectsFromArray:msg.GET_OUTPUT(@"orders")];
                int all = [msg.GET_OUTPUT(@"totallNum") intValue];
                self.more = self.ordersArray.count >= all? NO : YES;
                //[self saveCache];
                
            }
            
            
        }else if (msg.failed)
        {
        
             [self presentFailureTips:@"网络错误"];
        
        }
        
        
        [self sendUISignal:self.RELOADED];
        
        
    }
}

ON_MESSAGE3(HttpAPI, history_order, msg)
{
    
    if (msg.sending) {
        
        
        [self sendUISignal:self.RELOADING];
        
    }else {
        
        
        if (msg.succeed) {
            
            Status *sta = msg.GET_OUTPUT(@"status");
            if ([sta.code intValue] != 10000) {
                
                [self presentFailureTips:sta.message];
                
            }
            
            if (_page == 1) {
                
                [self.ordersArray removeAllObjects];
                self.ordersArray = msg.GET_OUTPUT(@"orders");
                
            }else{
                
                [self.ordersArray addObjectsFromArray:msg.GET_OUTPUT(@"orders")];
                //int all = [msg.GET_OUTPUT(@"totallNum") intValue];
                //self.more = self.ordersArray.count >= all? NO : YES;
                //[self saveCache];
                
            }
            
            
        }else if (msg.failed)
        {
            
            [self presentFailureTips:@"网络错误"];
            
        }
        
        
        [self sendUISignal:self.RELOADED];
        
        
    }
}


@end